#ifndef __RINGBUF_H__
#define __RINGBUF_H__

#define MAX_FRAME_CNT 	   100
#define MAX_RINGBUF_LEN  (1024*1024*2)  
//#define MOD_RINGBUF_LEN(x)   (x == MAX_RINGBUF_LEN ? 0:x)
#define MOD_RINGBUF_LEN(x)   (x & (MAX_RINGBUF_LEN - 1))

#include <memory.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <signal.h>
#include<limits.h>


struct ringbuf;
typedef struct ringbuf ringbuf;
typedef void (*RINGBUFDeallocator)(ringbuf* rbuf);
#pragma pack(8)
struct ringbuf
{
    uint32_t size;
    uint8_t* pdata;
    uint32_t pread;
    uint32_t pwrite;
    uint8_t empty;
    uint8_t full;
    uint32_t lengthsave;
    uint8_t leave_frame;
    uint32_t leave_frame_len[MAX_FRAME_CNT];
    uint8_t wf_index;
    uint8_t rf_index;
};
#pragma pack()


#ifdef __cplusplus
 extern "C" {
#endif


int rbuf_initial(ringbuf *rbuf,uint32_t size);
uint32_t write_rbuf(ringbuf *rbuf,uint8_t *data,uint32_t length);
uint32_t read_rbuf(ringbuf *rbuf,uint8_t *data,uint32_t length);
uint8_t is_rbuf_empty(ringbuf *rbuf);
uint8_t is_rbuf_full(ringbuf *rbuf);
uint32_t free_space(ringbuf *rbuf);
uint32_t get_leavesave(ringbuf *rbuf);
int8_t get_frame_cnt(ringbuf *rbuf);
uint8_t push_frame(ringbuf *rbuf,uint32_t length);
uint32_t pop_frame_len(ringbuf *rbuf);
uint8_t is_frame_cnt_full(ringbuf *rbuf);
int32_t move_rbuf_len(ringbuf *rbuf,int32_t len);
int32_t rbuf_uninitial(ringbuf *rbuf);
uint8_t is_frame_empty(ringbuf *rbuf);
uint8_t is_frame_full(ringbuf *rbuf);
int reset_rbuf(ringbuf *rbuf);

#ifdef __cplusplus
}

#endif

#endif /* __RINGBUF_H_ */


